package com.zdwl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zdwl.dao.SysUserMapper;
import com.zdwl.pojo.SysUser;
import com.zdwl.pojo.SysUserExample;
import com.zdwl.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Author: Professor_Kong
 * date: 2018/3/20
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    public Map findAll(int page, int limit , SysUser user) {
        PageHelper.startPage(page,limit);
        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample.createCriteria();
        if(user.getId()!=null&& !user.getId().equals("")){
            criteria.andIdEqualTo(user.getId());
        }
        if(user.getUsername()!=null && !"".equals(user.getUsername())){
            criteria.andUsernameLike(user.getUsername()+"%");
        }
        if(user.getPhone()!=null && !"".equals(user.getPhone())){
            criteria.andPhoneLike(user.getPhone()+"%");
        }
        sysUserExample.setOrderByClause("id asc");
        List<SysUser> userList = sysUserMapper.selectByExample(sysUserExample);

        PageInfo<SysUser> pageInfo = new PageInfo<>(userList);
        Map map = new HashMap();
        map.put("code",0);
        map.put("msg","ok");
        map.put("count",pageInfo.getTotal());
        map.put("data",userList);
        return map;
    }



    @Override
    public SysUser loginUser(SysUser us) {
        SysUser u = new SysUser();
        SysUserExample userExample = new SysUserExample();
        SysUserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(us.getUsername());
        List<SysUser> userList = sysUserMapper.selectByExample(userExample);
        if(userList!=null && userList.size()>0){
            return userList.get(0);
        }else {
            return null;
        }
    }


    @Override
    public void saveUser(SysUser user) {
        if(user.getId()!=null){
            sysUserMapper.updateByPrimaryKey(user);
        }else {
            Random random = new Random();
            int i = random.nextInt();
            UUID uuid = UUID.randomUUID();
            String substring = uuid.toString().substring(0, 3);
            user.setId(((int)Math.random()*100)+substring);
            user.setAddress("北京市顺义区");
            user.setCreateTime(new Date());
            user.setEmail("logwto@163.com");
            user.setPhone("15064857654");
            sysUserMapper.insert(user);
        }
    }

    @Override
    public SysUser findUserByUserName(String username) {
        SysUserExample userExample = new SysUserExample();
        SysUserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<SysUser> userList = sysUserMapper.selectByExample(userExample);
        if(userList!=null && userList.size()>0){
            return userList.get(0);
        }else {
            return null;
        }
    }

    @Override
    public void delUserById(SysUser user) {
        sysUserMapper.deleteByPrimaryKey(user.getId());
    }

    @Override
    public List<SysUser> getUserByIds(String id) {

        SysUserExample userExample = new SysUserExample();
        SysUserExample.Criteria criteria = userExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<SysUser> userList = sysUserMapper.selectByExample(userExample);
        return userList;
    }

    @Override
    public void delUserByUsers(List<SysUser> list) {
        if(list!=null && list.size() > 0){
            for(SysUser sysUser : list){
                sysUserMapper.deleteByPrimaryKey(sysUser.getId());
            }
        }

    }
}
